package cn.fl.project.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.preloan.IPrjPrdInvoiceApiService;
import cn.fl.preloan.dto.GetPrjPrdInvoiceListPageResponse;
import cn.fl.commons.result.ResponseData;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.condition.PrjPrdPaySchMQueryCondition;
import cn.fl.project.dto.PrjPrdPaySchMDTO;
import cn.fl.project.service.*;
import cn.fl.project.vo.PrjPrdDtlVO;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.preloan.condition.PrjPrdInvoiceQueryCondition;
import cn.fl.preloan.dto.PrjPrdInvoiceDTO;
import cn.fl.project.transform.PrjPrdInvoiceVOTransform;
import cn.fl.project.vo.PrjPrdInvoiceVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;

/**
 * @Description: 机动车发票 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjPrdInvoiceServiceImpl implements IPrjPrdInvoiceService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IPrjPrdInvoiceService.class);
//	@Autowired
//	private IProjectApiService projectApiService;

	@Autowired
	private IPrjPrdInvoiceApiService prjPrdInvoiceApiService;
	@Autowired
	private IPrjPrdPaySchMApiService prjPrdPaySchMApiService;

	@Autowired
	private IPrjPrdDtlService prjPrdDtlService;
	/**
	 * TODO 自定义 API 暴露服务
	 */
	

	@Override
	public PrjPrdInvoiceVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		ResponseData<PrjPrdInvoiceDTO> responseData = prjPrdInvoiceApiService.selectById(id);
		PrjPrdInvoiceDTO dto = responseData.getData();
		PrjPrdInvoiceVO vo = new PrjPrdInvoiceVO();
		if(dto!=null){
			BeanUtils.copyProperties(dto,vo);
			if(dto.getInvoicedate()!=null){
				vo.setInvoicedate(DateUtils.formatDateToString(dto.getInvoicedate(),"yyyy-MM-dd"));
			}
		}
		return vo;
		//PrjPrdInvoiceDTO dto = projectApiService.selectPrjPrdInvoiceById(id);//this.自定义ApiService.selectById(id);
		//return PrjPrdInvoiceVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjPrdInvoiceVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		BaseLogUtils.newLogger("保存发票信息，入参={}", JSON.toJSONString(entity)).info();
		PrjPrdInvoiceDTO dto = new PrjPrdInvoiceDTO();
		BeanUtils.copyProperties(entity,dto);
		if (StringUtils.isNotEmpty(entity.getPaySchNo())) {
			PrjPrdDtlQueryCondition queryCondition = new PrjPrdDtlQueryCondition();
			queryCondition.setPaySchNo(entity.getPaySchNo());
			queryCondition.setIsDel(0);
			List<PrjPrdDtlVO> prjPrdDtlVOS = prjPrdDtlService.selectList(queryCondition);
			if (CollectionUtils.isEmpty(prjPrdDtlVOS)) {
				throw new BaseException(entity.getPaySchNo() + "项目产品明细数据不存在！");
			}
			dto.setPaySchId(prjPrdDtlVOS.get(0).getPaySchId());
			dto.setPrjPrdDtlId(prjPrdDtlVOS.get(0).getId());
		}
		if (StringUtils.isNotBlank(entity.getInvoicedate())) {
			dto.setInvoicedate(DateUtils.formatStringToDate(entity.getInvoicedate(), "yyyy-MM-dd"));
		}
		ResponseData<Long> responseData = prjPrdInvoiceApiService.add(dto);
		return responseData.getData();
		//return this.projectApiService.addPrjPrdInvoice(PrjPrdInvoiceVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		List<Long> list = new ArrayList<>();
		list.add(id);
		ResponseData responseData = prjPrdInvoiceApiService.deleteByIds(list);
		if(ResponseData.SUCCESS_CODE.equals(responseData.getCode())){
			return true;
		}else {
			return false;
		}
		//return this.projectApiService.removePrjPrdInvoiceById(id);
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(PrjPrdInvoiceVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		BaseLogUtils.newLogger("修改发票信息，入参={}", JSON.toJSONString(entity)).info();
		PrjPrdInvoiceDTO dto = new PrjPrdInvoiceDTO();
		BeanUtils.copyProperties(entity,dto);
		if(StringUtils.isNotBlank(entity.getInvoicedate())){
			dto.setInvoicedate(DateUtils.formatStringToDate(entity.getInvoicedate(),"yyyy-MM-dd"));
		}
		if (StringUtils.isNotEmpty(entity.getPaySchNo())) {
			PrjPrdDtlQueryCondition queryCondition = new PrjPrdDtlQueryCondition();
			queryCondition.setPaySchNo(entity.getPaySchNo());
			queryCondition.setIsDel(0);
			List<PrjPrdDtlVO> prjPrdDtlVOS = prjPrdDtlService.selectList(queryCondition);
			if (CollectionUtils.isEmpty(prjPrdDtlVOS)) {
				throw new BaseException(entity.getPaySchNo() + "项目产品明细数据不存在！");
			}
			dto.setPaySchId(prjPrdDtlVOS.get(0).getPaySchId());
			dto.setPrjPrdDtlId(prjPrdDtlVOS.get(0).getId());
		}
		ResponseData responseData = prjPrdInvoiceApiService.modify(dto);
		if(ResponseData.SUCCESS_CODE.equals(responseData.getCode())){
			return true;
		}else {
			return false;
		}
//		return this.projectApiService.modifyPrjPrdInvoice(PrjPrdInvoiceVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeInvoice (Long fileId) throws BaseException {
		if(StringUtils.isEmpty(fileId)){
			BaseLogUtils.info(logger, "removeInvoice", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		ResponseData responseData = prjPrdInvoiceApiService.deleteByFileId(String.valueOf(fileId));
		if(ResponseData.SUCCESS_CODE.equals(responseData.getCode())){
			return true;
		}else {
			return false;
		}
//		PrjPrdInvoiceQueryCondition invoiceQueryCondition = new PrjPrdInvoiceQueryCondition();
//		invoiceQueryCondition.setFileId(fileId);
//		List<PrjPrdInvoiceVO> prjPrdInvoiceList = this.searchPrjPrdInvoiceList(invoiceQueryCondition);
//		if (CollectionUtils.isNotEmpty(prjPrdInvoiceList)) {
//			Long id = prjPrdInvoiceList.get(0).getId();
//			return this.projectApiService.removePrjPrdInvoiceById(id);
//		}
//		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		ResponseData<GetPrjPrdInvoiceListPageResponse> responseData = prjPrdInvoiceApiService.selectListPage(page.getEveryPage(),page.getCurrentPage(),
				(PrjPrdInvoiceQueryCondition)page.getCondition(),CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		GetPrjPrdInvoiceListPageResponse pageDto = responseData.getData();
		List<PrjPrdInvoiceVO> list = new ArrayList<>();
		Pager pager = new Pager();
		if(null!=pageDto ){
			if( CollectionUtils.isNotEmpty(pageDto.getList())){
				for(PrjPrdInvoiceDTO dto : pageDto.getList()){
					PrjPrdInvoiceVO vo = new PrjPrdInvoiceVO();
					BeanUtils.copyProperties(dto,vo);
					if(dto.getInvoicedate()!=null){
						vo.setInvoicedate(DateUtils.formatDateToString(dto.getInvoicedate(),"yyyy-MM-dd"));
					}
					list.add(vo);
				}
			}
			pager.setTotalCount(pageDto.getTotalCount());
		}
		pager.setResultList(list);
		return pager;
		//return this.projectApiService.searchPrjPrdInvoiceListPage(page,CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}
	/**
	 * 查询发票信息
	 * lxb
	 * @param condition
	 * @return
	 * @throws BaseException
	 */
	@Override
	public List<PrjPrdInvoiceVO> searchPrjPrdInvoiceList(PrjPrdInvoiceQueryCondition condition) throws BaseException{

		ResponseData<List<PrjPrdInvoiceDTO>> responseData = prjPrdInvoiceApiService.selectList(condition);
		List<PrjPrdInvoiceVO> list = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(responseData.getData())){
			for(PrjPrdInvoiceDTO dto : responseData.getData()){
				PrjPrdInvoiceVO vo = new PrjPrdInvoiceVO();
				BeanUtils.copyProperties(dto,vo);
				if(dto.getInvoicedate()!=null){
					vo.setInvoicedate(DateUtils.formatDateToString(dto.getInvoicedate(),"yyyy-MM-dd"));
				}
				list.add(vo);
			}
		}
		return list;
//		return PrjPrdInvoiceVOTransform.toVOList(this.projectApiService.searchPrjPrdInvoiceList(condition));
	}
	/**
	 * lxb
	 * 评审流程信审审核查看列表页面
	 */
	@Override
	public List<PrjPrdInvoiceVO> searchListForXS(PrjPrdInvoiceQueryCondition condition) throws BaseException{
		ResponseData<List<PrjPrdInvoiceDTO>> responseData = prjPrdInvoiceApiService.selectListForXS(condition);
		List<PrjPrdInvoiceVO> list = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(responseData.getData())){
			for(PrjPrdInvoiceDTO dto : responseData.getData()){
				PrjPrdInvoiceVO vo = new PrjPrdInvoiceVO();
				BeanUtils.copyProperties(dto,vo);
				if(dto.getInvoicedate()!=null){
					vo.setInvoicedate(DateUtils.formatDateToString(dto.getInvoicedate(),"yyyy-MM-dd"));
				}
				list.add(vo);
			}
		}
		return list;
//		return PrjPrdInvoiceVOTransform.toVOList(this.projectApiService.searchListForXS(condition));
	}

	@Override
	public List<PrjPrdInvoiceVO> searchPrjPrdInvoiceListByCondition (PrjPrdInvoiceQueryCondition condition) throws BaseException {
		ResponseData<List<PrjPrdInvoiceDTO>> responseData = prjPrdInvoiceApiService.selectListByCondition(condition);
		List<PrjPrdInvoiceVO> list = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(responseData.getData())){
			for(PrjPrdInvoiceDTO dto : responseData.getData()){
				PrjPrdInvoiceVO vo = new PrjPrdInvoiceVO();
				BeanUtils.copyProperties(dto,vo);
				if(dto.getInvoicedate()!=null){
					vo.setInvoicedate(DateUtils.formatDateToString(dto.getInvoicedate(),"yyyy-MM-dd"));
				}
				list.add(vo);
			}
		}
		return list;
//		return PrjPrdInvoiceVOTransform.toVOList(this.projectApiService.searchPrjPrdInvoiceListByCondition(condition));
	}

	@Override
	public boolean modifyAudStsByPrjIdAndPaySchNo(String prjId, String paySchNo, String audStsCd) throws BaseException {

		ResponseData responseData = prjPrdInvoiceApiService.modifyAudStsByPrjIdAndPaySchNo(prjId,paySchNo,audStsCd);
		if(ResponseData.SUCCESS_CODE.equals(responseData.getCode())){
			return true;
		}else {
			return false;
		}
	}
}
